package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import view.generaldesign.DesignLayout;

/*
 @author Mathias and Sonny Sandberg
 */
public class OrderComment {

    private JPanel commentPanel;
    private JTextPane tPaneComment;
    private Order order;

    public OrderComment(Order view) {
        order = view;

        commentPanel = new JPanel(null);
        commentPanel.setBounds(0, 0, 300, 130);
        commentPanel.setOpaque(false);
        commentPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Bemærkninger", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));

        tPaneComment = new JTextPane();
        tPaneComment.setBounds(10, 25, 280, 90);
        tPaneComment.setBorder(DesignLayout.T_FIELD_RED);
        tPaneComment.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tPaneComment.setFont(DesignLayout.STANDARD);
        tPaneComment.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                order.getOrderObject().setComment(tPaneComment.getText());
            }
        });

        commentPanel.add(tPaneComment);

        commentPanel.setVisible(false);
    }
    
    public void setFieldsFromModel()
    {
        model.Order orderObject = order.getOrderObject();
        StyledDocument commentDoc = tPaneComment.getStyledDocument();
        try
        {
            commentDoc.insertString(0, orderObject.getComment(), null);
        } 
        catch (BadLocationException ex)
        {
            ExceptionManager.handleBadLocationException(ex);
        }
    }

    public JPanel getPanel() {
        return commentPanel;
    }
}